feat(email): drain queued sys_email rows so app inserts actually deliver#2002
Merged
Conversation
Apps that can only `api.write` (e.g. sandboxed action bodies, which expose no `api.email`) cannot reach the email service — the only thing they can do is INSERT a `sys_email` row. Until now such rows sat at `status:'queued'` forever: declared-but-never-delivered. The transport (and the queued→sent transition) only ran inside `EmailService.send()`, which a raw insert never invokes. Treat a `sys_email` row inserted as `status:'queued'` with no `message_id` as an OUTBOX entry: an afterInsert hook delivers it through the live transport and finalizes the same row in place (sent + message_id + sent_at, or failed + error). Delivery is deferred past the insert so transport I/O never runs inside the insert's transaction, and the row is re-read + re-checked under system context before sending (idempotent against concurrent drains). Rows that the service's own `send()` inserts are tagged managed (EmailService.isServiceManaged) and skipped by the hook, so they are delivered exactly once by send() — never double-sent. - email-service.ts: managedRowIds guard; extract deliverNormalized() shared by send() and the new deliverPersistedRow(); rowToNormalized() helper. - email-plugin.ts: register the sys_email afterInsert drain hook. - tests: managed-guard active at insert moment; deliverPersistedRow delivers without inserting a 2nd row + fails cleanly on bodyless rows; rowToNormalized. Verified empirically in HotCRM: a raw INSERT of a queued sys_email now transitions queued→sent with a transport message_id within ~600ms; before, the identical insert stayed queued forever. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 2 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (verified empirically, not by reading specs)
An app that can only `api.write` — e.g. a sandboxed action body, which exposes no `api.email` and cannot reach the queue — has exactly one way to "send" mail: INSERT a `sys_email` row. But the transport (and the `queued → sent` transition) only ran inside `EmailService.send()`. A raw insert never invokes it, so the row sat at `status:'queued'` forever — declared-but-never-delivered, not even log-simulated.
This was caught while auditing a real consumer (HotCRM's `send_email` action): it raw-inserts a queued `sys_email` and writes a "completed Email" timeline activity — i.e. the UI claimed delivery that never happened.
Fix
Treat a `sys_email` row inserted as `status:'queued'` with no `message_id` as an outbox entry. A `sys_email` afterInsert hook delivers it through the live transport and finalizes the same row in place (`sent` + `message_id` + `sent_at`, or `failed` + `error`).
Changes
Verification
🤖 Generated with Claude Code